// pages/mine/mySize/newSize/newSize.js
const app = getApp();
const util = require("../../../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    customOptionList: [],
    partIndex: "",
    targetIndex: "",
    bodyFeature: [{
        id: 0,
        text: "背部厚实",
        isChecked: true
      },
      {
        id: 1,
        text: "溜肩严重",
        isChecked: false
      },
      {
        id: 2,
        text: "手臂特长",
        isChecked: false
      },
      {
        id: 3,
        text: "肩膀特别宽",
        isChecked: false
      },
      {
        id: 4,
        text: "胸部肌肉特别多",
        isChecked: false
      },
      {
        id: 5,
        text: "脖子短",
        isChecked: false
      }
    ],
    legend: [{
        id: 0,
        img: "http://jiangping.goobye.cn/zhengmian.png",
        text: "正面照"
      },
      {
        id: 1,
        img: "http://jiangping.goobye.cn/cemian.png",
        text: "侧面照"
      },
      {
        id: 2,
        img: "http://jiangping.goobye.cn/beimian.png",
        text: "背面照"
      }
    ],
    customList: [],
    name: [],
    height: [],
    age: [],
    weight: [],
    feature: [],
    note: [],
    img: [],
    files: [],
    pics: []
  },

  // 选择对应体型
  selectPart: function(e) {
    // console.log(e.currentTarget.dataset.index)
    let index = e.currentTarget.dataset.index
    this.setData({
      partIndex: index
    })
  },

  // 选择对应目标
  selectPartStyle: function(e) {
    let that = this
    let index = e.currentTarget.dataset.index
    that.setData({
      targetIndex: index
    })
    for (let i = 0; i < that.data.customOptionList[that.data.partIndex].paramOptions.length; i++) {
      if (that.data.targetIndex == i) {
        that.data.customOptionList[that.data.partIndex].paramOptions[i].select = false
      } else {
        that.data.customOptionList[that.data.partIndex].paramOptions[i].select = true
      }
      this.setData(this.data)
    }
  },

  // 选择身体特征
  selectFeature: function(e) {
    let that = this
    let index = e.currentTarget.dataset.index
    let item = that.data.bodyFeature[index]
    item.isChecked = !item.isChecked
    that.setData({
      bodyFeature: that.data.bodyFeature
    });
    let arr = []
    for (let i = 0; i < that.data.bodyFeature.length; i++) {
      if (that.data.bodyFeature[i].isChecked == true) {
        let text = that.data.bodyFeature[i].text
        arr.push(text)
      }
    }
    let feature = {
      paramName: "身体特征",
      option: arr
    }
    that.data.feature.push(feature)
    that.setData(that.data)
    // console.log(that.data.feature)
  },

  // 上传图片
  chooseImage: function(e) {
    var that = this,
      pics = that.data.pics
    wx.chooseImage({
      count: 5,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        // console.log(res)
        that.setData({
          pics: res.tempFilePaths
        })
        that.setData({
          files: that.data.files.concat(res.tempFilePaths)
        });
        // console.log(that.data.files)
        let img = {
          paramName: "上传图片",
          option: that.data.files
        }
        that.data.img.push(img)
        that.setData(that.data)
        // console.log(that.data.img)
        let imgsrc = res.tempFilePaths
        pics = pics.concat(imgsrc)
      }
    })
  },
  //删除图片
  deleteImg:function(res){
    var files = this.data.files
    for(var i=0;i<files.length;i++){
      if (i == res.currentTarget.dataset.id){
        files.splice(i,1)
        break
      }
    
    }
    this.setData({
      files: files
    })
  },
  previewImage: function(e) {
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.files // 需要预览的图片http链接列表
    })
  },



  // 获取姓名
  nameInput: function(e) {
    let that = this
    let name = {
      paramName: "姓名",
      option: e.detail.value
    }
    that.data.name.push(name)
    // that.data.sname = e.detail.value
    that.setData(that.data)
    console.log(that.data.name)
  },

  // 获取身高
  heightInput: function(e) {
    let that = this
    let height = {
      paramName: "身高",
      option: e.detail.value
    }
    that.data.height.push(height)
    that.data.sheight = e.detail.value
    that.setData(that.data)
    // console.log(height)
  },

  // 获取年龄
  ageInput: function(e) {
    let that = this
    let age = {
      paramName: "年龄",
      option: e.detail.value
    }
    that.data.age.push(age)
    that.data.sage = e.detail.value
    that.setData(that.data)
    // console.log(age)
  },

  // 获取体重
  weightInput: function(e) {
    let that = this
    let weight = {
      paramName: "体重",
      option: e.detail.value
    }
    that.data.weight.push(weight)
    that.data.sweight = e.detail.value
    that.setData(that.data)
    // console.log(weight)
  },

  // 获取身体特征备注
  featureNote: function(e) {
    let that = this
    let note = {
      paramName: "身体特征备注",
      option: e.detail.value
    }
    that.data.note.push(note)
    that.data.snote = e.detail.value
    that.setData(that.data)
    // console.log(note)
  },

  //多张图片上传
  uploadimg: function(data) {
    var that = this,
      i = data.i ? data.i : 0, //当前上传的哪张图片
      success = data.success ? data.success : 0, //上传成功的个数
      fail = data.fail ? data.fail : 0; //上传失败的个数
    wx.uploadFile({
      url: data.url,
      filePath: data.path[i],
      name: 'file', //这里根据自己的实际情况改
      formData: data.data, //这里是上传图片时一起上传的数据
      success: (resp) => {
        success++; //图片上传成功，图片上传成功的变量+1
        // console.log(resp)
        // console.log(i);
        //这里可能有BUG，失败也会执行这里,所以这里应该是后台返回过来的状态码为成功时，这里的success才+1
      },
      fail: (res) => {
        fail++; //图片上传失败，图片上传失败的变量+1
        // console.log('fail:' + i + "fail:" + fail);
      },
      complete: () => {
        // console.log(i);
        i++; //这个图片执行完上传后，开始上传下一张
        if (i == data.path.length) { //当图片传完时，停止调用          
          // console.log('执行完毕');
          // console.log('成功：' + success + " 失败：" + fail);
        } else { //若图片还没有传完，则继续调用函数
          // console.log(i);
          data.i = i;
          data.success = success;
          data.fail = fail;
          that.uploadimg(data);
        }
      }
    });
  },

  // 点击完成
  finishBtn: function(e) {
    let that = this

    // 获取用户选中的体型元素
    let list = that.data.customOptionList
    for (let i = 0; i < list.length; i++) {
      for (let j = 0; j < list[i].paramOptions.length; j++) {
        if (list[i].paramOptions[j].select == false) {
          let custom = {
            paramName: list[i].paramName,
            option: list[i].paramOptions[j].option
          }
          that.data.customList.push(custom)
        }
      }
    }

    let last = that.data
    if (last.feature.length == 0) {
      last.feature = [{
        paramName: "身体特征",
        option: ["背部厚实"]
      }]
    }
    if (last.note.length == 0) {
      last.note = [{
        paramName: "身体特征备注",
        option: "无"
      }]
    }
    // console.log(last.name)
    if (last.name.length == 0 ) {
      wx.showModal({
        title: '请填写姓名',
        showCancel: false
      })
      return
    }
     if (last.height.length == 0) {
      wx.showModal({
        title: '请填写身高',
        showCancel: false
      })
      return
    } 
    if (last.age.legnth == 0) {
      wx.showModal({
        title: '请填写年龄',
        showCancel: false
      })
      return
    }
    if (last.weight.length == 0) {
      wx.showModal({
        title: '请填写体重',
        showCancel: false
      })
      return
    } else {
      // let arr = []
      //  arr = arr.push(last.name)
      // arr = arr.push(last.height)
      //  that.setData(that.data)
      //  console.log(arr)
       last.name = last.name[last.name.length - 1]
       last.height = last.height[last.height.length - 1]
       last.age = last.age[last.age.length - 1]
       last.weight = last.weight[last.weight.length - 1]
       last.note = last.note[last.note.length - 1]
      last.feature = last.feature[last.feature.length - 1]
      last.customList = last.customList.concat(last.name, last.height, last.age, last.weight, last.feature, last.note)
      that.setData(that.data)
      console.log(last.customList)
      // 将尺寸信息传回后台
      let msgUrl = app.globalData.baseUrl + "weixin/addSize"
      let data = {
        cid: wx.getStorageSync('cid'),
        msg: that.data.customList
      }
      // console.log(data)
      util.requestLoading(msgUrl, data, "GET").then(res => {
        // console.log(res)
        // 上传图片
        let pics = that.data.pics
        console.log(pics)
        if (pics.length == 0){
          wx.navigateBack({
            delta: 1
          })
        } else {
          that.uploadimg({
            url: app.globalData.baseUrl + 'weixin/addSizePics',
            path: pics,
            data: {
              id: res.data.id
            }
          })
          wx.navigateBack({
            delta: 1
          })
        }
      })
    }
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this
    // 请求尺寸参数
    let sizeUrl = app.globalData.baseUrl + "weixin/sizeParams"
    util.requestLoading(sizeUrl, null, "GET").then(res => {
      // console.log(res)
      that.setData({
        customOptionList: res.data.customOptionList
      })
    })
  },
})